/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.musictalkserver.model.connectr;

import com.musictalk.message.model.*;
import com.musictalkserver.model.manager.ManageClientThread;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AIDEN
 */
public class ClientThread extends Thread {

    private Socket userSocket;
    private User logUser;

    public ClientThread(Socket userSocket, User logUser) {
        this.logUser = logUser;
        this.userSocket = userSocket;

    }

    public Socket getUserSocket() {
        return userSocket;
    }

    @Override
    public void run() {
        while (true) {
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(userSocket.getInputStream());
                Object temp = ois.readObject();
                Message m = (Message) temp;
                if (m.getMsgType() == Message.MSG_NORMAL) {
                    NormalMessage conMsg = (NormalMessage) temp;
                    //在此处寻找好友并通讯
                    ClientThread friendThread = ManageClientThread.gerClientThread(conMsg.getReceiver().getUID());
                    System.out.println(ManageClientThread.getClientMap().containsKey(conMsg.getReceiver().getUID()));
                    ObjectOutputStream oos = new ObjectOutputStream(friendThread.getUserSocket().getOutputStream());
                    oos.writeObject(conMsg);
                } else if (m.getMsgType() == Message.MSG_CHAT_ROOM) {
                    RoomChat rMsg = (RoomChat) temp;
                    System.out.println(rMsg.getSender() + ":" + rMsg.getMsg());
                    Iterator it = ManageClientThread.getClientMap().keySet().iterator();
                    while (it.hasNext()) {
                        ObjectOutputStream oos = new ObjectOutputStream(ManageClientThread.gerClientThread((String) it.next()).getUserSocket().getOutputStream());
                        oos.writeObject(rMsg);
                    }
                } else if (m.getMsgType() == Message.MSG_LOGOUT) {
                    Logout logout = (Logout) temp;
                    ManageClientThread.removeThread(logout.getLogoutID());
                } else if (m.getMsgType() == Message.MSG_FILE) {
//                    FileMessage fmsg = (FileMessage) temp;
//                    File file = new File("D:\\dzfbq.exe");
//                    FileOutputStream out = new FileOutputStream(file);
//                    byte[] buffer = new byte[1024 * 8];
//                    int len = -1;
//                    while (ois.readObject() != -1) {
//                        out.write(buffer, 0, len);
//                    }
                }else if (m.getMsgType()==Message.MSG_REQUEST_ONLINE_FRIEND) {
                    RequestOnline ronline = (RequestOnline)temp;
                    OnlineFriend onlineFriend = new OnlineFriend(ronline.getSenderID());
                    Iterator it = ManageClientThread.getClientMap().keySet().iterator();
                    while (it.hasNext()) {
                        onlineFriend.addOnline((String) it.next());
                    }
                    ObjectOutputStream oos = new ObjectOutputStream(ManageClientThread.gerClientThread(ronline.getSenderID()).getUserSocket().getOutputStream());
//                    try{
                    oos.writeObject(onlineFriend);
//                    }catch(Exception ex){
//                        System.out.println("nonono");
//                    }
                }

            } catch (ClassNotFoundException ex) {
                ManageClientThread.removeThread(logUser.getUID());
            } catch (IOException ex) {
                //Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
                ManageClientThread.removeThread(logUser.getUID());
            }
        }
    }
}
